package com.hdax.dm.service;

import com.hdax.dm.dto.CommonResponse;
import com.hdax.dm.dto.DtoUtil;
import com.hdax.dm.dto.item.FloorItemDto;
import com.hdax.dm.dto.item.HotItemDto;
import com.hdax.dm.entity.base.DmImage;
import com.hdax.dm.entity.item.DmCinema;
import com.hdax.dm.entity.item.DmCinemaSeat;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.feign.base.DmImageFeign;
import com.hdax.dm.feign.item.DmCinemaFeign;
import com.hdax.dm.feign.item.DmItemFeign;
import com.hdax.dm.utils.MokeImage;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IndexQiziService {

    @Autowired
    private DmItemFeign itemFeign;
    @Autowired
    private DmImageFeign dmImageFeign;
    @Autowired
    private DmCinemaFeign cinemaFeign;

    //亲子轮播图
    public CommonResponse<List<HotItemDto>> getCarouselData(Long itemTypeId){
        List<DmItem> dmItems = itemFeign.getFloorOrder(itemTypeId,5l);
        List<HotItemDto> hotItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            HotItemDto hotItemDto = new HotItemDto();
            BeanUtils.copyProperties(dmItem,hotItemDto);
            //查询图片
            DmImage dmImage = dmImageFeign.getImage(1L,1L,dmItem.getId());
            hotItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(2) :dmImage.getImgUrl());
            hotItemDtos.add(hotItemDto);
        });
        return DtoUtil.returnSuccess(hotItemDtos);
    }

    //精彩聚焦
    public CommonResponse<List<FloorItemDto>> getWonderfulData(Long itemTypeId){
        List<DmItem> dmItems = itemFeign.getFloorOrder(itemTypeId,8L);
        List<FloorItemDto> floorItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            FloorItemDto floorItemDto = new FloorItemDto();
            BeanUtils.copyProperties(dmItem,floorItemDto);
            //查询图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            floorItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(1) :dmImage.getImgUrl());
            //查询地址
            DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
            floorItemDto.setAreaId(dmCinema.getId());
            floorItemDto.setAreaName(dmCinema.getAreaName());
            floorItemDto.setAddress(dmCinema.getAddress());
            floorItemDtos.add(floorItemDto);
        });
        return DtoUtil.returnSuccess(floorItemDtos);
    }

    //根据年龄段查询节目
    public CommonResponse<FloorItemDto> getParentChild(Long ageGroup){
        List<DmItem> dmItems = itemFeign.byAgeGroup(ageGroup);
        List<FloorItemDto> floorItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            FloorItemDto floorItemDto = new FloorItemDto();
            BeanUtils.copyProperties(dmItem,floorItemDto);
            //查询图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            floorItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(3) :dmImage.getImgUrl());
            //查询地址
            DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
            floorItemDto.setAreaId(dmCinema.getId());
            floorItemDto.setAreaName(dmCinema.getAreaName());
            floorItemDto.setAddress(dmCinema.getAddress());
            floorItemDtos.add(floorItemDto);
        });
        return DtoUtil.returnSuccess(floorItemDtos);
    }

    //获取热门排行数据信息
    public CommonResponse<FloorItemDto> getPopularSearch(Long itemTypeId){
        List<DmItem> dmItems = itemFeign.getFloorOrder(itemTypeId,7L);
        List<FloorItemDto> floorItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            FloorItemDto floorItemDto = new FloorItemDto();
            BeanUtils.copyProperties(dmItem,floorItemDto);
            //查询图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            floorItemDto.setImgUrl(dmImage == null ? MokeImage.getImage(1) :dmImage.getImgUrl());
            //查询地址
            DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
            floorItemDto.setAreaId(dmCinema.getId());
            floorItemDto.setAreaName(dmCinema.getAreaName());
            floorItemDto.setAddress(dmCinema.getAddress());
            floorItemDtos.add(floorItemDto);
        });
        return DtoUtil.returnSuccess(floorItemDtos);
    }

    //获取页面广告地址及信息页面
    public CommonResponse<FloorItemDto> getAdimgUrl(Long itemTypeId){
        List<DmItem> dmItems = itemFeign.getFloorOrder(itemTypeId,3L);
        List<FloorItemDto> floorItemDtos = new ArrayList<>();
        dmItems.forEach(dmItem -> {
            FloorItemDto floorItemDto = new FloorItemDto();
            BeanUtils.copyProperties(dmItem,floorItemDto);
            //查询图片
            DmImage dmImage = dmImageFeign.getImage(2L,1L,dmItem.getId());
            floorItemDto.setImgUrl(MokeImage.getImage(3));
            //查询地址
            DmCinema dmCinema = cinemaFeign.getCinema(dmItem.getCinemaId());
            floorItemDto.setAreaId(dmCinema.getId());
            floorItemDto.setAreaName(dmCinema.getAreaName());
            floorItemDto.setAddress(dmCinema.getAddress());
            floorItemDtos.add(floorItemDto);
        });
        return DtoUtil.returnSuccess(floorItemDtos);
    }

    //日历
    public CommonResponse<List<Map<String,Object>>> getCalendarData(Long itemTypeId){
        List<Map<String,Object>> list = new ArrayList<>();
        List<DmItem> dmItems = itemFeign.findItemByType(itemTypeId);
        //根据开始时间分组
        Map<String, List<DmItem>> map = dmItems.stream().collect(Collectors.groupingBy(DmItem::getStartTime));
        map.forEach((key,value)->{
            Map<String,Object> returnMap = new HashMap<>();
            String[] times = key.substring(0,10).split("-");
            String day = StringUtils.join(times);
            List<FloorItemDto> floorItemDtos = new ArrayList<>();
            value.forEach(dmItem->{
                FloorItemDto floorItemDto = new FloorItemDto();
                BeanUtils.copyProperties(dmItem,floorItemDto);
                //查询图片
                floorItemDto.setImgUrl(MokeImage.getImage(1));
                floorItemDtos.add(floorItemDto);
            });
            returnMap.put("day",day);
            returnMap.put("itemList",floorItemDtos);
            list.add(returnMap);
        });
        return DtoUtil.returnSuccess(list);
    }
}
